﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.RDLC

Partial Class DocTakeUpAuction
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        If Not Page.IsPostBack Then

            CBEBack.ConfirmText = "You are about to navigate away from this page. " & vbCrLf & "Changes will be discard!"
            CBEDownload.ConfirmText = "All the changes will be saved before you download the document!"

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

            If Not Session("id") Is Nothing Then
                lblDocId.Text = Me.Session("id").ToString
                pnlEntry.Visible = True
                pnlPreview.Visible = False
                LoadEntry()
            Else
                pnlEntry.Visible = True
                pnlPreview.Visible = False
            End If
        End If
    End Sub

    Private Sub GetStaff(ByVal rep As Repeater)
        Dim TempDT As New Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT StaffId,Name FROM AW_Staff WHERE StatusDelete = 0 ORDER BY Name"
        objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        rep.DataSource = TempDT
        rep.DataBind()
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()
            Dim DocAucId As String
            If lblDocId.Text = String.Empty Then
                objDB.dbVariable.SQLString = "INSERT INTO AW_DocTakeUpAuction(DocAucId, DocAucTitle, REN, Lister, DocDate, " & _
                                            "Premise, Price, AuctionDate, Remarks, Bank, ContactPerson, ContactNo, FaxNo, " & _
                                            "CPRemarks, Commission, Purchaser, NRIC, Disbursement, Address, PContactNo, " & _
                                            "CreateBy, CreateDate, UpdateBy, UpdateDate, StatusDelete) VALUES(@DocAucId, " & _
                                            "@DocAucTitle, @REN, @Lister, @DocDate, @Premise, @Price, @AuctionDate, @Remarks, " & _
                                            "@Bank, @ContactPerson, @ContactNo, @FaxNo, @CPRemarks, @Commission, @Purchaser, " & _
                                            "@NRIC, @Disbursement, @Address, @PContactNo, @CreateBy, @CreateDate, @UpdateBy, " & _
                                            "@UpdateDate, @StatusDelete)"
                DocAucId = Guid.NewGuid.ToString
                lblUpdated.Text = "Created By : " & GetName() & " on " & Format(Now(), "dd-MMM-yyyy")
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, DBNull.Value)
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, DBNull.Value)
            Else
                objDB.dbVariable.SQLString = "UPDATE AW_DocTakeUpAuction SET DocAucTitle=@DocAucTitle, REN=@REN, Lister=@Lister, " & _
                                            "DocDate=@DocDate, Premise=@Premise, Price=@Price, AuctionDate=@AuctionDate, " & _
                                            "Remarks=@Remarks, Bank=@Bank, ContactPerson=@ContactPerson, ContactNo=@ContactNo, " & _
                                            "FaxNo=@FaxNo, CPRemarks=@CPRemarks, Commission=@Commission, Purchaser=@Purchaser, " & _
                                            "NRIC=@NRIC, Disbursement=@Disbursement, Address=@Address, PContactNo=@PContactNo, " & _
                                            "UpdateBy=@UpdateBy, UpdateDate=@UpdateDate WHERE DocAucId=@DocAucId"

                DocAucId = lblDocId.Text
                lblUpdated.Text = "Last Updated By : " & GetName() & " on " & Format(Now(), "dd-MMM-yyyy")
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, Now())
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            End If
            objDB.AddParameter("DocAucId", Data.SqlDbType.UniqueIdentifier, New Guid(DocAucId))
            objDB.AddParameter("DocAucTitle", Data.SqlDbType.NVarChar, txtTitle.Text.Trim.ToUpper)
            If txtREN.Text = String.Empty Then
                objDB.AddParameter("REN", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("REN", Data.SqlDbType.NVarChar, txtREN.Text.Trim)
            End If
            If txtLister.Text = String.Empty Then
                objDB.AddParameter("Lister", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Lister", Data.SqlDbType.NVarChar, txtLister.Text.Trim)
            End If
            If txtDate.Text = String.Empty Then
                objDB.AddParameter("DocDate", Data.SqlDbType.DateTime, DBNull.Value)
            Else
                objDB.AddParameter("DocDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtDate.Text.Trim, "dd-MMM-yyyy", Nothing))
            End If
            If txtPremise.Text = String.Empty Then
                objDB.AddParameter("Premise", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Premise", Data.SqlDbType.NVarChar, txtPremise.Text.Trim)
            End If
            If txtPrice.Text = String.Empty Then
                objDB.AddParameter("Price", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Price", Data.SqlDbType.NVarChar, txtPrice.Text.Trim)
            End If
            If txtAuctionDate.Text = String.Empty Then
                objDB.AddParameter("AuctionDate", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("AuctionDate", Data.SqlDbType.NVarChar, txtAuctionDate.Text.Trim)
            End If
            If txtRemarks.Text = String.Empty Then
                objDB.AddParameter("Remarks", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Remarks", Data.SqlDbType.NVarChar, txtRemarks.Text.Trim)
            End If
            If txtBank.Text = String.Empty Then
                objDB.AddParameter("Bank", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Bank", Data.SqlDbType.NVarChar, txtBank.Text.Trim)
            End If
            If txtContactPerson.Text = String.Empty Then
                objDB.AddParameter("ContactPerson", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("ContactPerson", Data.SqlDbType.NVarChar, txtContactPerson.Text.Trim)
            End If
            If txtContact.Text = String.Empty Then
                objDB.AddParameter("ContactNo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("ContactNo", Data.SqlDbType.NVarChar, txtContact.Text.Trim)
            End If
            If txtFax.Text = String.Empty Then
                objDB.AddParameter("FaxNo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("FaxNo", Data.SqlDbType.NVarChar, txtFax.Text.Trim)
            End If
            If txtCommission.Text = String.Empty Then
                objDB.AddParameter("Commission", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Commission", Data.SqlDbType.NVarChar, txtCommission.Text.Trim)
            End If
            If txtCPRemarks.Text = String.Empty Then
                objDB.AddParameter("CPRemarks", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("CPRemarks", Data.SqlDbType.NVarChar, txtCPRemarks.Text.Trim)
            End If
            If txtPurchaser.Text = String.Empty Then
                objDB.AddParameter("Purchaser", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Purchaser", Data.SqlDbType.NVarChar, txtPurchaser.Text.Trim)
            End If
            If txtNRIC.Text = String.Empty Then
                objDB.AddParameter("NRIC", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("NRIC", Data.SqlDbType.NVarChar, txtNRIC.Text.Trim)
            End If
            If txtDisbursement.Text = String.Empty Then
                objDB.AddParameter("Disbursement", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Disbursement", Data.SqlDbType.NVarChar, txtDisbursement.Text.Trim)
            End If
            If txtAddress.Text = String.Empty Then
                objDB.AddParameter("Address", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Address", Data.SqlDbType.NVarChar, txtAddress.Text.Trim)
            End If
            If txtPContactNo.Text = String.Empty Then
                objDB.AddParameter("PContactNo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("PContactNo", Data.SqlDbType.NVarChar, txtPContactNo.Text.Trim)
            End If
            objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now())
            objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
            objDB.ExecuteQuery()
            objDB.DbTransCommit()
            objDB.CloseConnection()
            lblDocId.Text = DocAucId
        Catch ex As Exception
            objDB.DbTransRollback()
            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.CloseConnection()
            End If
            lblMsgError.Text = ex.Message
            divError.Visible = True
        End Try
    End Sub

    Private Function GetName() As String
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Dim StaffName As String = String.Empty

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT Name FROM AW_Staff WHERE StaffId=@StaffId"
        objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
        objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        For Each dtrow As Data.DataRow In TempDT.Rows
            If dtrow.Item("Name").ToString <> "" Then
                StaffName = dtrow.Item("Name").ToString
            End If
        Next

        Return StaffName
    End Function

    Private Sub LoadEntry()

        Dim TempDT As New Data.DataTable
        TempDT = LoadDocTakeUpAuction()
        For Each dtrow As Data.DataRow In TempDT.Rows
            txtTitle.Text = dtrow.Item("DocAucTitle").ToString
            txtREN.Text = dtrow.Item("REN").ToString
            txtDate.Text = Format(dtrow.Item("DocDate"), "dd-MMM-yyyy")
            txtLister.Text = dtrow.Item("Lister").ToString
            txtPremise.Text = dtrow.Item("Premise").ToString
            txtPrice.Text = String.Format("{0:#,##0.00}", dtrow.Item("Price"))
            txtAuctionDate.Text = Format(dtrow.Item("AuctionDate"), "dd-MMM-yyyy")
            txtRemarks.Text = dtrow.Item("Remarks").ToString
            txtBank.Text = dtrow.Item("Bank").ToString
            txtContactPerson.Text = dtrow.Item("ContactPerson").ToString
            txtContact.Text = dtrow.Item("ContactNo").ToString
            txtFax.Text = dtrow.Item("FaxNo").ToString
            txtCommission.Text = String.Format("{0:#,##0.00}", dtrow.Item("Commission"))
            txtCPRemarks.Text = dtrow.Item("CPRemarks").ToString
            txtPurchaser.Text = dtrow.Item("Purchaser").ToString
            txtNRIC.Text = dtrow.Item("NRIC").ToString
            txtDisbursement.Text = String.Format("{0:#,##0.00}", dtrow.Item("Disbursement"))
            txtAddress.Text = dtrow.Item("Address").ToString
            txtPContactNo.Text = dtrow.Item("PContactNo").ToString
            lblUpdated.Text = "Last Updated By : " & dtrow.Item("CreateBy").ToString & " on " & Format(dtrow.Item("CreateDate"), "dd-MMM-yyyy")
        Next

    End Sub

    Private Function LoadDocTakeUpAuction() As Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT d.DocAucTitle, d.REN, d.Lister, d.Premise, d.Price, d.AuctionDate, d.Remarks, d.ContactPerson, " & _
                                    "d.Bank, d.ContactNo, d.Commission, d.CPRemarks, d.Purchaser, d.NRIC, d.Disbursement, d.Address, " & _
                                    "d.PContactNo, ISNULL(d.UpdateDate, d.CreateDate) as CreateDate, " & _
                                    "ISNULL(u.Initial, c.Initial) as CreateBy, d.StatusDelete, d.DocDate, d.FaxNo FROM AW_DocTakeUpAuction d " & _
                                    "LEFT JOIN AW_Staff u ON d.UpdateBy = u.StaffId LEFT JOIN AW_Staff c ON " & _
                                    "d.CreateBy = c.StaffId WHERE DocAucId=@DocAucId"
        objDB.AddParameter("DocAucId", Data.SqlDbType.UniqueIdentifier, New Guid(lblDocId.Text))
        objDB.FillData(TempDT, "DocTakeUpAuction", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()
        Return TempDT
    End Function

    Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        pnlEntry.Visible = False
        pnlPreview.Visible = True

        Dim TempDT As New Data.DataTable
        Dim CommissionDT As New Data.DataTable
        Dim GrandTotal As Decimal = 0

        'TempDT = LoadDocTakeUpAuction()
        DeclareColumnTempDT(TempDT)
        Dim MyRow As Data.DataRow
        MyRow = TempDT.NewRow()
        MyRow(0) = txtTitle.Text.Trim.ToUpper
        MyRow(1) = txtREN.Text.Trim
        MyRow(2) = txtLister.Text.Trim
        If txtDate.Text = String.Empty Then
            MyRow(3) = DBNull.Value
        Else
            MyRow(3) = DateTime.ParseExact(txtDate.Text.Trim, "dd-MMM-yyyy", Nothing)
        End If
        MyRow(4) = txtPremise.Text.Trim
        MyRow(5) = txtPrice.Text.Trim
        If txtAuctionDate.Text = String.Empty Then
            MyRow(6) = DBNull.Value
        Else
            MyRow(6) = DateTime.ParseExact(txtAuctionDate.Text.Trim, "dd-MMM-yyyy", Nothing)
        End If
        MyRow(7) = txtRemarks.Text.Trim
        MyRow(8) = txtBank.Text.Trim
        MyRow(9) = txtContactPerson.Text.Trim
        MyRow(10) = txtContact.Text.Trim
        MyRow(11) = txtFax.Text.Trim
        MyRow(12) = txtCPRemarks.Text.Trim
        MyRow(13) = txtCommission.Text.Trim
        MyRow(14) = txtPurchaser.Text.Trim
        MyRow(15) = txtNRIC.Text.Trim
        MyRow(16) = txtDisbursement.Text.Trim
        MyRow(17) = txtAddress.Text.Trim
        MyRow(18) = txtPContactNo.Text.Trim
        MyRow(19) = GetName()
        MyRow(20) = Now()
        TempDT.Rows.Add(MyRow)

        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptDocTakeUpAuction.rdlc", "DocTakeUpAuction")
            TempDT.TableName = "dsDocTakeUpAuction_AW_DocTakeUpAuction"
            objRDLC.AddDatabase(TempDT)
            ReportViewer1.LocalReport.DataSources.Clear()
            ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource(TempDT.TableName, TempDT))
            ReportViewer1.LocalReport.Refresh()
        End If
    End Sub

    Private Sub DeclareColumnTempDT(ByVal MyDT As Data.DataTable)
        Dim DocAucTitle As Data.DataColumn = New Data.DataColumn("DocAucTitle")
        DocAucTitle.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(DocAucTitle)
        Dim REN As Data.DataColumn = New Data.DataColumn("REN")
        REN.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(REN)
        Dim Lister As Data.DataColumn = New Data.DataColumn("Lister")
        Lister.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Lister)
        Dim DocDate As Data.DataColumn = New Data.DataColumn("DocDate")
        DocDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(DocDate)
        Dim Premise As Data.DataColumn = New Data.DataColumn("Premise")
        Premise.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Premise)
        Dim Price As Data.DataColumn = New Data.DataColumn("Price")
        Price.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Price)
        Dim AuctionDate As Data.DataColumn = New Data.DataColumn("AuctionDate")
        AuctionDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(AuctionDate)
        Dim Remarks As Data.DataColumn = New Data.DataColumn("Remarks")
        Remarks.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Remarks)
        Dim Bank As Data.DataColumn = New Data.DataColumn("Bank")
        Bank.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Bank)
        Dim ContactPerson As Data.DataColumn = New Data.DataColumn("ContactPerson")
        ContactPerson.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(ContactPerson)
        Dim ContactNo As Data.DataColumn = New Data.DataColumn("ContactNo")
        ContactNo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(ContactNo)
        Dim FaxNo As Data.DataColumn = New Data.DataColumn("FaxNo")
        FaxNo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(FaxNo)
        Dim CPRemarks As Data.DataColumn = New Data.DataColumn("CPRemarks")
        CPRemarks.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(CPRemarks)
        Dim Commission As Data.DataColumn = New Data.DataColumn("Commission")
        Commission.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Commission)
        Dim Purchaser As Data.DataColumn = New Data.DataColumn("Purchaser")
        Purchaser.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Purchaser)
        Dim NRIC As Data.DataColumn = New Data.DataColumn("NRIC")
        NRIC.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(NRIC)
        Dim Disbursement As Data.DataColumn = New Data.DataColumn("Disbursement")
        Disbursement.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Disbursement)
        Dim Address As Data.DataColumn = New Data.DataColumn("Address")
        Address.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Address)
        Dim PContactNo As Data.DataColumn = New Data.DataColumn("PContactNo")
        PContactNo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(PContactNo)
        Dim CreateBy As Data.DataColumn = New Data.DataColumn("CreateBy")
        CreateBy.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(CreateBy)
        Dim CreateDate As Data.DataColumn = New Data.DataColumn("CreateDate")
        CreateDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(CreateDate)
    End Sub

    Protected Sub btnBackEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackEntry.Click
        pnlEntry.Visible = True
        pnlPreview.Visible = False
    End Sub

    Protected Sub btnSearchREN_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearchREN.Click
        GetStaff(repREN)
        mpeRENEntry.Show()
    End Sub

    Protected Sub repLister_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repLister.ItemCommand
        If e.CommandName = "SelectType" Then
            Dim Lister As String = CType(repLister.Items(e.CommandArgument).FindControl("lnkRepLister"), LinkButton).Text
            txtLister.Text = Lister
        End If
    End Sub

    Protected Sub btnSearchLister_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearchLister.Click
        GetStaff(repLister)
        mpeListerEntry.Show()
    End Sub

    Protected Sub repREN_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repREN.ItemCommand
        If e.CommandName = "SelectType" Then
            Dim REN As String = CType(repREN.Items(e.CommandArgument).FindControl("lnkRepREN"), LinkButton).Text
            txtREN.Text = REN
        End If
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If Session("Back") Is Nothing Then
            Response.Redirect("Documentation.aspx")
        Else
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            Response.Redirect(arr(0).ToString)
        End If
    End Sub

    Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Dim TempDT As New Data.DataTable

        BtnSave_Click(sender, e)

        Dim CurrentUser As String = GetName()
        Dim PrintStr As String = "Printed by " & CurrentUser & " on " & Format(Now(), "dd-MMM-yyyy")

        TempDT = LoadDocTakeUpAuction()

        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptDocTakeUpAuction.rdlc", "DocTakeUpAuction")
            TempDT.TableName = "dsDocTakeUpAuction_AW_DocTakeUpAuction"
            objRDLC.AddDatabase(TempDT)
            objRDLC.AddParameter("PrintStr", PrintStr)
            objRDLC.ExportReport(RDLC.ExportTypeFormat.PDF, HttpContext.Current)
        End If
    End Sub
End Class
